package com.heima.article.listener;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.pojos.ApArticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class ArticleCommentListener {

    @Autowired
    private ApArticleService apArticleService;


    /**
     * 监听评论数量
     * @param message
     */
    @KafkaListener(topics = ArticleConstants.ARTICLE_COMMENT)
    public void onMessage(String message){
        Long articleId = Long.valueOf(message);
        ApArticle apArticle = apArticleService.getById(articleId);
        //获取评论数量
        Integer comment = apArticle.getComment();
        comment= comment==null? 1 : (comment+1);
        apArticle.setComment(comment);
        apArticleService.updateById(apArticle);
    }

}
